;(function ($, win, d, undefined) {
    function getPositions(proBox) {
        var o = proBox.e,
            e = proBox.elements.content,
            s = proBox.s,
            p = s.position;

        p = p.split('|');
        if (p.length === 1) {
            p[1] = 'center';
        }
        var w = s.fixed ? $(win).width() : o.width(),
            h = s.fixed ? $(win).height() : o.height(),
            ew = e.outerWidth(),
            eh = e.outerHeight(),
            r = {width:ew, height:eh};

        $.each(p, function (i, v) {
            switch (v) {
                case 'left':    r.left = 0;     break;
                case 'top':     r.top = 0;      break;
                case 'right':   r.left = w-ew;  break;
                case 'bottom':  r.top = h-eh;   break;
                case 'center':
                    if (i && ew < w) {
                        r.left = w/2 - ew/2;
                    } else if (!i && eh < h) {
                        r.top = h/2 - eh /2;
                    }
                    break;
            }
        });
        return r;
    }
    
    function animateTo(e, c) {
        c = c || 0;
        p = $.data(e.container, 'state');
        var tmp = '';
        if (c) {
            switch ($.data(e.container, 'motion')) {
                case 'slideUp':     tmp = {top: p.top};     break;
                case 'slideDown':   tmp = {top: p.top};     break;
                case 'slideLeft':   tmp = {left: p.left};   break;
                case 'slideRight':  tmp = {left: p.left};   break;
                default:            tmp = {opacity: 0};     break;
            }

            e.content.animate(tmp, function () {
                e.container.remove();
            });

            if (e.mask.length > 0) {
                e.mask.fadeOut();
            }
        } else {
            switch ($.data(e.container, 'motion')) {
                case 'slideUp':
                    p.top = p.top + p.height;
                    tmp = {'top': p.top - p.height};
                    break;
                case 'slideDown':
                    p.top = p.top - p.height;
                    tmp = {'top': p.top + p.height};
                    break;
                case 'slideLeft':
                    p.left = p.left+p.width;
                    tmp = {'left': p.left - p.width};
                    break;
                case 'slideRight':
                    p.left = p.left-p.width;
                    tmp = {'left': p.left + p.width};
                    break;
            }

            if ('' === tmp) {
                e.content.css(p).fadeIn();
            } else {
                p.display = 'block';
                e.content.css(p).animate(tmp);
            }

            if (e.mask.length > 0) {
                e.mask.fadeIn();
            }
        }
    }

    function proboxShow(probox) {
        var e = probox.elements,
            s = probox.s,
            image = $('img', e.content);

        $.data(e.container, 'motion', s.motion);
        if (image.length) {
            image.load(function () {
                if (!--image.length) {
                    e.content.ready(function () {
                        $.data(e.container, 'state', getPositions(probox));
                        animateTo(e);
                    });
                }
            });
        } else {
            e.content.ready(function () {
                $.data(e.container, 'state', getPositions(probox));
                animateTo(e);
            });
        }
    }

    function ProBox(o, c) {
        this.e = o;
        this.s = $.extend({}, this.defaults, c);
        this.init();
    }

    ProBox.prototype = {
        // 初始配置参数
        defaults: {
            type     : 0,                // 显示样式 0加载框，1onfirm确认提示框，2消息框, 3内容框, 4加载页面
            id       : 'probox',         // 弹出层ID
            title    : '',               // 标题
            html     : '',               // 展示内容
            fixed    : 1,                // 是否浮动
            loader   : {},               // loading样式设置
            mask     : '',               // 遮罩层样式设置
            zindex   : 0,                // 弹出层深度 0 不设置
            timer    : 0,                // 自动关闭时间 0 不关闭 单位 秒
            motion   : 'fade',           // 动画效果 fade渐隐渐现 slide+方位Up,Down,Left,Right
            position : 'center|center',  // 定位展示位置 X轴|Y轴
            close    : '',               // 绑定关闭按钮 多个使用','分割
            btn      : ['取消','确定']
        },
        // 初始化
        init: function () {
            var _t = this,
                ct = '#' + _t.s.id;
            if (!$(ct).length) {
                _t.container = $('<div id="'+_t.s.id+'" class="probox"><div class="main"><div class="content"></div></div></div>').appendTo(_t.e);
            } else {
                _t.container = $(ct);
                $('.content', _t.container).html('');
            }
            // 保存对象
            _t.elements = {
                container   : _t.container,
                main        : $('.main', _t.container),
                content     : $('.content', _t.container),
                mask        : $('.mask', _t.container)
            };

            var setting = _t.s,
                pos = setting.fixed ? 'fixed' : 'absolute';
            // 绝对定位判断父元素是否relative
            if (!setting.fixed && _t.e.css('position') !== 'relative') {
                _t.e.css({position:'relative'});
            }
            _t.container.css({position:pos});
            // 设置显示层级
            if (setting.zindex) {
                _t.container.css({zIndex: setting.zindex});
            }
            // 调用展示类型
            _t.ContentFactory();

            proboxShow(_t);
            _t.AutoClose();
            _t.bindClose();
            return _t.e;
        },
        // 设置主体内容
        ContentFactory: function () {
            switch (this.s.type) {
                case 1:
                    this.Create_Confirm();
                    break;
                case 2:
                    this.Create_Message();
                    break;
                case 3:
                    this.Creat_Content();
                    break;
                case 4:
                    this.Create_Iframe();
                    break;
                default:
                    this.Create_Loading();
                    break;
            }
        },
        // 设置弹出层遮罩背景
        Create_Mask: function () {
            var _t = this;
            var setting = $.extend({show:1, color:'0,0,0', opc:'0.4'}, _t.s.mask);
            if (setting.show) {
                _t.mask = $('<div class="mask"></div>').appendTo(_t.container);
                _t.mask.css({backgroundColor:'rgba('+setting.color+','+setting.opc+')'});
                _t.elements.mask = $('.mask', _t.container);
            }
        },
        // 设置加载动画
        Create_Loading: function () {
            //var setting  = $.extend(defaults, this.s.loader);
            var loader = $('<div class="loader-inner line-scale-pulse-out"><div></div><div></div><div></div><div></div><div></div></div>');

            loader.appendTo(this.elements.content);
            if (this.s.title.length) {
                $('<p>'+this.s.title+'</p>').insertAfter(loader);
            }
            this.Create_Mask();
            this.elements.content.addClass('loading');
        },
        // 确认提示框
        Create_Confirm: function () {
            var _t = this,
                e = _t.elements,
                s = _t.s;

            e.content.addClass('pro-confirm').append('<p>'+s.html+'</p>');
            e.buttons = $('<div class="btns"></div>').appendTo(e.content);
            if (s.btn.length) {
                var b = '';
                $.each(s.btn, function (i, o) {
                    b = $('<button class="">'+o+'</button>');
                    b.css({'width': parseFloat(1/s.btn.length) * 100 + '%'});
                    if (typeof s['btn'+i] === 'undefined' && o === '取消') {
                        b.on('click', function () {
                            _t.doClose();
                        });
                    }
                    if (typeof s['btn'+i] === 'function') {
                        b.on('click', function () {
                            _t.doClose();
                            s['btn'+i]();
                        });
                    }
                    b.appendTo(e.buttons);
                });
            }
            _t.Create_Mask();
        },
        // 消息提示框
        Create_Message: function () {
            var _t = this,
                e = _t.elements,
                s = _t.s;

            e.close = $('<i class="pmclose"></i>').appendTo(e.content);
            if (s.title.length) {
                e.title = $('<div class="title"></div>').appendTo(e.content);
                e.title.append(s.title);
            }
            e.content.addClass('pro-message').append('<p>'+s.html+'</p>');
            _t.Create_Mask();
            e.mask.on('click', function () {
                _t.doClose();
            });
        },
        // 内容框
        Creat_Content: function () {
            var _t = this;
            var e = _t.elements;
            var s = _t.s;

            if (s.title.length) {
                e.title = $('<div class="title"></div>').appendTo(e.content);
                e.title.append(s.title);
            }
            $(s.html).appendTo(e.content);
            _t.Create_Mask();
            e.mask.on('click', function () {
                _t.doClose();
            });
        },
        // iframe
        Create_Iframe: function () {
            var _t = this,
                e = _t.elements,
                s = _t.s;

            e.content.append('<iframe width="100%" height="100%" frameborder="0" marginheight="0" marginwidth="0" src="'+s.url+'"></iframe>');
            _t.Create_Mask();
            e.mask.on('click', function () {
                _t.doClose();
            });
        },
        // 自动关闭
        AutoClose: function () {
            var _t = this;
            if (_t.s.timer) {
                setTimeout(function () { _t.doClose() }, _t.s.timer*1000);
            }
        },
        // 绑定关闭按钮
        bindClose: function () {
            var _t = this,
                s = _t.s;

            if (s.close.length) {
                var c = s.close.split(',');
                $.each(c, function (i, v) {
                    $(v).on('click', function () {
                        _t.doClose();
                    });
                });
            } else {
                if (_t.elements.close === undefined) {
                    _t.elements.close = $('<div class="'+s.id+'close"></div>').insertAfter(_t.elements.content);
                }
                _t.elements.close.on('click', function () {
                    _t.doClose();
                });
            }
        },
        // 执行关闭
        doClose: function () {
            var _t = this;
            if (typeof _t.s.closeFun === 'function') {
                _t.s.closeFun();
            } else {
                animateTo(_t.elements, 1);
            }
        }
    };

    $.fn.proBox = function (options) {
        var _t = this,
            args = arguments;

        if (options === undefined || typeof options === 'object') {
            return _t.each(function () {
                if (!$.data(_t, 'plugin_proBox')) {
                    $.data(_t, 'plugin_proBox', new ProBox(_t, options));
                } else {
                    $.data(_t, 'plugin_proBox');
                }
            });
        } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
            var returns;
            this.each(function () {
                var instance = $.data(_t, 'plugin_proBox');
                if (instance instanceof ProBox && typeof instance[options] === 'function') {
                    returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1));
                }
            });
            return returns !== undefined ? returns : this;
        }
    };
    
    $.fn.proBox.close = function (option) {
        option = option || 'probox';
        $('.'+option+'close').click();
    }
})(jQuery, window, document);